<template>
  <div class="main-content">
    <h1 style="margin: 5px 0">机构门店业绩排名表</h1>
    <div class="screen-frame clearfix">
      <div class="col-lg-6">
        <div class="form-horizontal">
          <div class="form-group">
            <label class="col-md-3 col-sm-2 control-label">楼盘名称：</label>
            <div class="col-md-4 col-sm-5">
              <el-select class=""
                         v-model="params.premisesId"
                         filterable
                         placeholder="请选择"
                         :clearable="true"
                         style="width:100%">
                <el-option v-for="item in findPremisesList"
                           :key="item.premisesId"
                           :label="item.premisesName"
                           :value="item.premisesId">
                </el-option>
              </el-select>
            </div>
          </div>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-horizontal">
          <div class="form-group">
            <label class="col-md-3 col-sm-2 control-label">时间区间：</label>
            <div class="col-md-4 col-sm-5">
              <DatePicker type="date"
                          placeholder="开始日期"
                          v-model="params.beginDate"
                          style="width: 200px"></DatePicker>
            </div>
            <div class="col-md-4 col-sm-5">
              <DatePicker type="date"
                          placeholder="结束日期"
                          v-model="params.endDate"
                          style="width: 200px"></DatePicker>
            </div>
          </div>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-horizontal">
          <div class="form-group">
            <label class="col-md-3 col-sm-2 control-label">合作机构：</label>
            <div class="col-md-4 col-sm-5">
              <Select v-model="params.orgId"
                      filterable>
                <Option v-for="item in orgList"
                        v-model="item.agentCompany">{{ item.agentCompanyName }}</Option>
              </Select>
            </div>
          </div>
        </div>
      </div>
      <div class="col-lg-6">
        <div class="form-group">
          <label class="col-md-3 col-sm-2 control-label"></label>
          <div class="col-md-4 col-sm-5">
            <button type="button"
                    class="btn btn-theme"
                    v-on:click="list(1)">查询</button>
            <button type="button"
                    class="btn btn-theme"
                    v-if="exportBtnShow"
                    v-on:click="exportInfo"
                    v-res="{code:'menu_0007_0005_0001'}">导出</button>
            <button type="button"
                    class="btn btn-theme"
                    v-if="!exportBtnShow"
                    style="background-color: #999;"
                    v-on:click="exportRepeatMsg"
                    v-res="{code:'menu_0007_0005_0001'}">导出{{ timeOut }}</button>
          </div>
        </div>
      </div>
    </div>
    <div class="row"
         style="text-align: center;color: #f87242;"
         v-show="show">
      没有查询到相应的数据
    </div>
    <div class="table table-striped table-bordered"
         v-show="!show">

      <div class="table-responsive">
        <table class="table table-striped table-bordered min-width-790">
          <thead>
            <tr>
              <th width="4%">排名</th>
              <th width="10%">门店名</th>
              <th width="6%">当前经纪人</th>
              <th width="6%">累计经纪人</th>
              <th width="6%">报备客户</th>
              <th width="6%">报备成功客户</th>
              <th width="6%">到访客户</th>
              <th width="6%">认筹客户</th>
              <th width="6%">认购客户</th>
              <th width="6%">签约客户</th>
              <th width="6%">签约率</th>
              <th width="6%">签约业绩</th>
              <th width="6%">查看</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(row,index) in effectList">
              <td>{{index + 1}}</td>
              <td><a class="a-report"
                   @click="gotoGroupReport(row.id,row.orgId)">{{row.companyName}}</a></td>
              <td>{{row.countEight}}</td>
              <td>{{row.countSeven}}</td>
              <td>{{row.countOne}}</td>
              <td>{{row.countTwo}}</td>
              <td>{{row.countThree}}</td>
              <td>{{row.countFour}}</td>
              <td>{{row.countFive}}</td>
              <td>{{row.countSix}}</td>
              <td>{{utils.calculateSignRate(row.countTwo,row.countSix)}}</td>
              <td>{{row.dealAmount}}</td>
              <td><a class="a-report"
                   @click="gotoAgentReport(row.id)">查看经纪人业绩</a> </td>
            </tr>
            <tr class="result-total">
              <td colspan="2">合计</td>
              <td>{{resultTotal.countEight}}</td>
              <td>{{resultTotal.countSeven}}</td>
              <td>{{resultTotal.countOne}}</td>
              <td>{{resultTotal.countTwo}}</td>
              <td>{{resultTotal.countThree}}</td>
              <td>{{resultTotal.countFour}}</td>
              <td>{{resultTotal.countFive}}</td>
              <td>{{resultTotal.countSix}}</td>
              <td></td>
              <td>{{resultTotal.dealAmount}}</td>
              <td></td>
            </tr>
          </tbody>
        </table>
      </div>
      <div id="laypage_effect_report"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      params: {
        pageNo: 1,
        pageSize: 10,
        premisesId: "",
        beginDate: this.utils.getCurrentMonthFirst(),
        endDate: this.utils.getCurrentMonthLast(),
        storeName: "",
        orgId: "",
      },
      effectList: [],
      findPremisesList: [],
      orgList: [{ agentCompany: "", agentCompanyName: "全部" }],
      show: false,
      resultTotal: {
        countEight: 0,
        countSeven: 0,
        countOne: 0,
        countTwo: 0,
        countThree: 0,
        countFour: 0,
        countFive: 0,
        countSix: 0,
        dealAmount: 0,
      },
      exportBtnShow: true,
      timeOut: 10,
    };
  },
  mounted: function () {
    //加载时间与楼盘
    if (this.$route.params.premisesId) {
      this.params.premisesId = this.$route.params.premisesId;
    }

    if (this.$route.params.beginDate || this.$route.params.endDate) {
      this.params.beginDate = this.$route.params.beginDate;
      this.params.endDate = this.$route.params.endDate;
    }
    // this.list();
    this.findPremisesLists();
    this.findOrgList();
  },
  methods: {
    list: function (pageNo) {
      var loadIndex = layer.load();
      if (pageNo) {
        this.params.pageNo = 1;
      }

      var _self = this;
      var url = this.utilHelper.apiUrl + "/api/getStoreReport";
      var body = {};
      body.params = _self.params;
      body.params.beginDate = this.utils.dateFormat(
        _self.params.beginDate,
        "yyyy-MM-dd"
      );
      body.params.endDate = this.utils.dateFormat(
        _self.params.endDate,
        "yyyy-MM-dd"
      );
      _self.$http.post(url, body).then((response) => {
        layer.close(loadIndex);
        var body = response.body.resData;
        if (body.result == 0) {
          layer.msg("接口异常!");
        } else {
          _self.effectList = body.pageModel.rows;
          //统计数据
          _self.setResultTotoal();
          var totalPages = body.pageModel.totalPages;
          var current = body.pageModel.pageNumber;
          var total = body.pageModel.total;
          _self.show = total == 0 ? true : false;
          layui.laypage.render({
            elem: $("#laypage_effect_report"), //容器。值支持id名、原生dom对象，jquery对象,
            limit: _self.params.pageSize,
            curr: current,
            count: total,
            theme: "#f87242;", //皮肤
            first: "首页", //若不显示，设置false即可
            last: "尾页", //若不显示，设置false即可
            prev: "上一页", //若不显示，设置false即可
            next: "下一页", //若不显示，设置false即可
            limits: [10, 20, 50, 100, 200],
            layout: ["prev", "page", "next", "skip", "count", "limit"],
            jump: function (obj, first) {
              if (!first) {
                _self.params.pageNo = obj.curr;
                _self.params.pageSize = obj.limit;
                _self.list();
              }
            },
          });
        }
      });
    },
    findPremisesLists: function () {
      var _self = this;
      var url = this.utilHelper.apiUrl + "/api/getCaseMemberList";
      var body = {};
      body.params = {
        userId: window.localStorage.getItem("userId"),
      };
      _self.$http.post(url, body).then((response) => {
        console.log(response.body.resData.caseMemberList);
        _self.findPremisesList = response.body.resData.caseMemberList;
      });
    },
    findOrgList: function () {
      //getAgentOrgByDevOrg
      var _self = this;
      var url = this.utilHelper.apiUrl + "/api/getAgentOrgByDevOrg";
      var body = {};
      body.params = {
        userId: window.localStorage.getItem("userId"),
      };
      _self.$http.post(url, body).then((response) => {
        // _self.orgList  =  _self.orgList.concat( response.body.resData.agentOrgList);
        _self.orgList = response.body.resData.agentOrgList;
        this.params.orgId = this.$route.params.orgId;

        if (!this.params.orgId) {
          this.params.orgId = _self.orgList[0].agentCompany;
        }
        _self.list();
      });
    },
    gotoAgentReport: function (storeId) {
      var _self = this;
      this.$router.push({
        name: "agentReport",
        params: {
          beginDate: this.params.beginDate,
          endDate: this.params.endDate,
          premisesId: _self.params.premisesId,
          orgId: _self.params.orgId,
          storeId: storeId,
          source: "store",
        },
      });
    },
    gotoGroupReport: function (storeId, orgId) {
      var _self = this;
      this.$router.push({
        name: "groupReport",
        params: {
          beginDate: this.params.beginDate,
          endDate: this.params.endDate,
          premisesId: _self.params.premisesId,
          orgId: orgId,
          storeId: storeId,
        },
      });
    },
    exportInfo: function () {
      var _self = this;
      _self.exportBtnShow = false;
      var userId = window.localStorage.getItem("1_userId");
      var beginDate = this.utils.dateFormat(
        _self.params.beginDate,
        "yyyy-MM-dd"
      );
      var endDate = this.utils.dateFormat(_self.params.endDate, "yyyy-MM-dd");
      var premisesId = "";
      if (_self.params.premisesId) {
        premisesId = _self.params.premisesId;
      }
      var url =
        this.utilHelper.apiUrl +
        "/api/getStoreReportExport?params.userId=" +
        userId +
        "&params.beginDate=" +
        beginDate +
        "&params.endDate=" +
        endDate +
        "&params.premisesId=" +
        premisesId +
        "&params.pageNo=1" +
        "&params.loginType=1&params.pageSize=9999999&params.orgId=" +
        this.params.orgId +
        "&params.storeName=" +
        this.params.storeName;
      location.href = url;

      var exportInterval = setInterval(function () {
        if (_self.timeOut <= 0) {
          window.clearInterval(exportInterval);
          _self.timeOut = 10;
          _self.exportBtnShow = true;
        } else {
          _self.timeOut -= 1;
        }
      }, 1000);
    },
    exportRepeatMsg: function () {
      var _self = this;
      layer.msg(_self.timeOut + "秒后可再次导出,请稍等片刻!");
      return;
    },
    setResultTotoal() {
      var _self = this;
      _self.resultTotal = {
        countEight: 0,
        countSeven: 0,
        countOne: 0,
        countTwo: 0,
        countThree: 0,
        countFour: 0,
        countFive: 0,
        countSix: 0,
        dealAmount: 0,
      };
      for (var i in _self.effectList) {
        _self.resultTotal.countOne =
          Number(_self.resultTotal.countOne) +
          Number(_self.effectList[i].countOne);
        _self.resultTotal.countTwo =
          Number(_self.resultTotal.countTwo) +
          Number(_self.effectList[i].countTwo);
        _self.resultTotal.countThree =
          Number(_self.resultTotal.countThree) +
          Number(_self.effectList[i].countThree);
        _self.resultTotal.countFour =
          Number(_self.resultTotal.countFour) +
          Number(_self.effectList[i].countFour);
        _self.resultTotal.countFive =
          Number(_self.resultTotal.countFive) +
          Number(_self.effectList[i].countFive);
        _self.resultTotal.countSix =
          Number(_self.resultTotal.countSix) +
          Number(_self.effectList[i].countSix);
        _self.resultTotal.countSeven =
          Number(_self.resultTotal.countSeven) +
          Number(_self.effectList[i].countSeven);
        _self.resultTotal.countEight =
          Number(_self.resultTotal.countEight) +
          Number(_self.effectList[i].countEight);
        _self.resultTotal.dealAmount =
          Number(_self.resultTotal.dealAmount) +
          Number(_self.effectList[i].dealAmount);
      }
    },
  },
};
</script>